package com.it.travel.service.admin.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.it.travel.dao.LogsMapper;
import com.it.travel.pojo.Logs;
import com.it.travel.service.admin.AdminLogsService;
import com.it.travel.vo.Params.PageParams;
import com.it.travel.vo.Result;
import com.it.travel.vo.admin.AdminLogsVO;
import org.springframework.stereotype.Service;
import sun.rmi.runtime.Log;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class AdminLogsServiceImpl implements AdminLogsService {
    @Resource
    private LogsMapper logsMapper;

    @Override
    public void insertLogs(Logs logs) {

        logsMapper.insert(logs);


    }

    @Override
    public Result<?> getInfoPage(PageParams pageParams) {
        Page<Logs> page = new Page<>(pageParams.getIndex(), pageParams.getSize());
        Page<Logs> logsPage = logsMapper.selectPage(page, null);
        List<Logs> logsList = logsPage.getRecords();
        List<AdminLogsVO> list = new ArrayList<>();
        Map<String, Object> map = new HashMap<>();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (Logs logs : logsList) {
            AdminLogsVO adminLogsVO = new AdminLogsVO();
            String format = simpleDateFormat.format(new Date(logs.getCreateTime()));
            BeanUtil.copyProperties(logs, adminLogsVO);
            adminLogsVO.setFormatTime(format);
            list.add(adminLogsVO);
        }
        map.put("logsList", list);
        map.put("total", logsPage.getTotal());
        return Result.success(map, "yes");
    }
}
